package com.aggregate.pay.saobei.support.client;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aggregate.pay.saobei.bean.RefundData;
import com.aggregate.pay.saobei.bean.RefundResult;
import com.aggregate.pay.saobei.support.AbstractClient;
import com.aggregate.pay.saobei.support.Factory;
import com.aggregate.pay.saobei.support.Merch;
import com.aggregate.pay.saobei.support.utils.MD5;

/**
 * 申请退款
 * @author Moyq5
 * @date 2018年6月30日
 */
public class Refund extends AbstractClient<RefundData, RefundResult> {
	
	private static final Logger log = LoggerFactory.getLogger(Refund.class);
	
	private Merch merch;
	
	public Refund(Merch merch) {
		super(merch);
		this.merch = merch;
	}

	@Override
	protected Class<RefundResult> getResultClass() {
		return RefundResult.class;
	}

	@Override
	protected String getApiPath() {
		return Factory.getConfig().serverPath() + "/pay/100/refund";
	}

	@Override
	protected String sign(RefundData data) {
		// 按固定字段固定顺序生成签名
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		map.put("pay_ver", data.getPayVer());
		map.put("pay_type", data.getPayType());
		map.put("service_id", data.getServiceId());
		map.put("merchant_no", data.getMerchantNo());
		map.put("terminal_id", data.getTerminalId());
		map.put("terminal_trace", data.getTerminalTrace());
		map.put("terminal_time", data.getTerminalTime());
		map.put("refund_fee", data.getRefundFee());
		if (isEmpty(data.getOutTradeNo())) {
			data.setOutTradeNo("");
		}
		map.put("out_trade_no", data.getOutTradeNo());
		
		StringBuffer sb = new StringBuffer();
		for (Entry<String, Object> entry: map.entrySet()) {
			sb.append(entry.getKey() + "=" + (null == entry.getValue() ? "": String.valueOf(entry.getValue())) + "&");
		}
		sb.append("access_token=" + merch.accessToken());

		log.debug("签名内容：{}", sb.toString());
		String sign = MD5.MD5Encode(sb.toString()).toUpperCase();
		log.debug("签名值：{}", sign);
		data.setKeySign(sign);
		
		return sign;
	}

	@Override
	protected String sign(RefundResult result) {
		if (null == result.getReturnCode() || !result.getReturnCode().equals("01")) {
			return result.getKeySign();
		}
		// 按固定字段固定顺序生成签名
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		map.put("return_code", result.getReturnCode());
		map.put("return_msg", result.getReturnMsg());
		map.put("result_code", result.getResultCode());
		map.put("pay_type", result.getPayType());
		map.put("merchant_name", result.getMerchantName());
		map.put("merchant_no", result.getMerchantNo());
		map.put("terminal_id", result.getTerminalId());
		map.put("terminal_trace", result.getTerminalTrace());
		map.put("terminal_time", result.getTerminalTime());
		map.put("refund_fee", result.getRefundFee());
		map.put("end_time", result.getEndTime());
		map.put("out_trade_no", result.getOutTradeNo());
		map.put("out_refund_no", result.getOutRefundNo());
		StringBuffer sb = new StringBuffer();
		for (Entry<String, Object> entry: map.entrySet()) {
			sb.append(entry.getKey() + "=" + (null == entry.getValue() ? "": String.valueOf(entry.getValue())) + "&");
		}
		sb.delete(sb.length() - 1, sb.length());

		log.debug("签名内容：{}", sb.toString());
		String sign = MD5.MD5Encode(sb.toString()).toUpperCase();
		log.debug("签名值：{}", sign);
		
		return sign;
	}
	

	private boolean isEmpty(Object obj) {
		return null == obj || String.valueOf(obj).trim().isEmpty();
	}

}
